home *** CD-ROM | disk | FTP | other *** search
/ Programmer Power Tools / Programmer Power Tools.iso / progjrn / pj_7_6.arc / WINDEV.ARC / WINVUEFN.C < prev    next >
C/C++ Source or Header  |  1989-04-23  |  8KB  |  294 lines

  1. /* 
  2.  * Winvue function support module module
  3.  *
  4.  * Written by Bill Hall
  5.  * 3665  Benton Street, #66
  6.  * Santa Clara, CA 95051
  7.  *
  8.  */
  9.  
  10. #define NOCOMM        
  11. #define NOKANJI
  12. #define NOATOM
  13. #define NOBITMAP
  14. #define NOMINMAX
  15. #include <windows.h>
  16. #include <string.h>
  17. #include <stdlib.h>
  18. #include <ascii.h>
  19. #include <io.h>
  20.  
  21. #include <ttycls.h>
  22. #include "winvue.h"
  23.  
  24. /* local function declaractions */
  25. static void NEAR ShowAboutBox(HWND hWnd);
  26. static int NEAR CloseTheFile(BOOL);
  27. static void NEAR EnableMenus(HMENU hmenu, BOOL op);
  28.  
  29. /* read next buffer of data from file or close the file */
  30. void NEAR ProcessFile()
  31. {
  32.     int numread, size;
  33.     size = min(MWnd.MaxCols, MAX_STR_LEN);
  34.  
  35.   /* user cancelled the file display */
  36.     if (Break) {
  37.     hFile = CloseTheFile(TRUE);
  38.     Break = FALSE;
  39.     }
  40.     else if (!(Pause || IsIconic(MWnd.hWnd))) {
  41.       /* read next buffer of data */
  42.         if ((numread = read(hFile, instr, size)) == 0)
  43.       /* no more data (or perhaps a read error) */
  44.          hFile = CloseTheFile(FALSE);
  45.         else
  46.       /* post the buffer and its length for later processing */
  47.         PostMessage(MWnd.hWnd, WM_USER, numread, (LONG)(LPSTR)instr);
  48.     }
  49. }
  50.  
  51. /* close the file, enable inactive menus, and switch message processor */
  52. static int NEAR CloseTheFile(param)
  53. {
  54.     
  55.     HMENU hmenu = GetMenu(MWnd.hWnd);
  56.  
  57.     Pause = FALSE;        /* reset the pause button */
  58.     EnableMenus(hmenu, TRUE);   /* enable grayed menus */
  59.  
  60.     DoMessage = DoGetMessage;   /* change the message processing pointer */
  61.  
  62.   /* show the result to the user */
  63.     if (param)
  64.     ShowMessage(MWnd.hWnd,(char *)NULL,IDS_CANCEL,
  65.                     MB_OK | MB_ICONEXCLAMATION);
  66.     else
  67.     ShowMessage(MWnd.hWnd,(char *)NULL,IDS_DONE,
  68.                     MB_OK | MB_ICONASTERISK);
  69.     return(close(hFile));
  70. }
  71.  
  72. /* open the file, display its name, and change the message function pointer */
  73. int FAR OpenTheFile(char *pfilename)
  74. {
  75.  
  76.     int hfile;
  77.     char winname[30];
  78.     
  79.   /* open the file */
  80.     hfile = OpenFile(pfilename, (LPOFSTRUCT)&fstruct, OF_READ);
  81.  
  82.     if (hfile != -1) {
  83.       /* change the window title */
  84.     strcpy(winname, szWinTitle);
  85.         strcat(winname, strrchr(fstruct.szPathName,'\\') + 1);
  86.         SetWindowText(MWnd.hWnd, winname);
  87.       /* change message function pointer */
  88.     DoMessage = DoPeekMessage;
  89.     }
  90.     else 
  91.      /* open failed */
  92.     ShowMessage(MWnd.hWnd,pfilename,IDS_CANNOT_OPEN,
  93.                     MB_OK | MB_ICONEXCLAMATION);
  94.     return(hfile);
  95. }
  96.  
  97. /* 
  98.    Display a simple message box according to the message number.
  99.    Merge a string into the message if mergestr != NULL
  100. */
  101. void ShowMessage(HWND hWnd, char *mergestr, int msgnum, int style)
  102. {
  103.  
  104.     char szMsg[MAX_STR_LEN];
  105.     char szTemp[MAX_STR_LEN / 2];
  106.     HANDLE hInst = GetWindowWord(hWnd, GWW_HINSTANCE);
  107.  
  108.     if (mergestr) {
  109.     LoadString(hInst, msgnum, (LPSTR)szTemp, sizeof(szTemp));
  110.         DlgMergeStrings(szTemp, szFileNameSave, szMsg);
  111.     }
  112.     else
  113.         LoadString(hInst, msgnum, (LPSTR)szMsg, sizeof(szMsg));
  114.     
  115.   /* put up this simple message box */
  116.     MessageBox(hWnd,(LPSTR)szMsg, (LPSTR)szAppName, style);
  117.  
  118. }
  119.  
  120. /* create 'about' dialog box */
  121. static void NEAR ShowAboutBox(HWND hWnd)
  122. {
  123.  
  124.     FARPROC fp;
  125.     HANDLE hInstance = GetWindowWord(hWnd, GWW_HINSTANCE);
  126.  
  127.   /* make a proc instance for the about box window function */
  128.     fp = MakeProcInstance((FARPROC)AboutBoxProc, hInstance);
  129.   /* create a modal dialog box */
  130.     DialogBox(hInstance, MAKEINTRESOURCE(DT_ABOUT),hWnd,fp);
  131.   /* free the proc instance */
  132.     FreeProcInstance(fp);
  133. }
  134.  
  135. /* activate or disable certain menus during file display */
  136. static void NEAR EnableMenus(HMENU hmenu, BOOL op)
  137. {
  138.  
  139.     EnableMenuItem(hmenu, IDM_FILENAME, (op ? MF_ENABLED : MF_GRAYED));
  140.     EnableMenuItem(hmenu, IDM_SYSFONT, (op ? MF_ENABLED : MF_GRAYED));
  141.     EnableMenuItem(hmenu, IDM_OEMFONT, (op ? MF_ENABLED : MF_GRAYED));
  142.     EnableMenuItem(hmenu, IDM_MASK, (op ? MF_ENABLED : MF_GRAYED));
  143.  
  144.     EnableMenuItem(hmenu, IDM_CANCEL, (op ? MF_GRAYED : MF_ENABLED));
  145.     EnableMenuItem(hmenu, IDM_PAUSE, (op ? MF_GRAYED : MF_ENABLED));
  146.     EnableMenuItem(hmenu, IDM_RESUME, (op ? MF_GRAYED : MF_ENABLED));
  147. }
  148.  
  149. /* process the menu */
  150. void NEAR WndCommand(HWND hWnd, WORD wParam)
  151. {
  152.  
  153.     HMENU hMenu = GetMenu(hWnd);
  154.     HANDLE hInstance = GetWindowWord(hWnd, GWW_HINSTANCE);
  155.     int result;
  156.  
  157.     switch (wParam) {
  158.  
  159.     case IDM_ABOUT:
  160.         ShowAboutBox(hWnd);
  161.         break;
  162.  
  163.     case IDM_FILENAME:
  164.         result = DialogBox(hInstance, MAKEINTRESOURCE(DT_OPEN),hWnd,
  165.             MakeProcInstance((FARPROC)DlgFnOpen, hInstance));
  166.       /* file open succeeded */
  167.         if (result) {
  168.             if ((hFile = OpenTheFile(szFileNameSave)) != -1) {
  169.             TTYClear(&MWnd);
  170.             EnableMenus(hMenu, FALSE);
  171.             Pause = FALSE;
  172.             }
  173.         }
  174.         break;
  175.  
  176.       /* suspend file display */
  177.     case IDM_PAUSE:
  178.         Pause = TRUE;
  179.         CheckMenuItem(hMenu, IDM_PAUSE, MF_CHECKED);
  180.         CheckMenuItem(hMenu, IDM_RESUME, MF_UNCHECKED);
  181.         break;
  182.  
  183.       /* resume file display */
  184.     case IDM_RESUME:
  185.         Pause = FALSE;
  186.         CheckMenuItem(hMenu, IDM_RESUME, MF_CHECKED);
  187.         CheckMenuItem(hMenu, IDM_PAUSE, MF_UNCHECKED);
  188.         break;
  189.  
  190.       /* cancel file display */
  191.     case IDM_CANCEL:
  192.         Break = TRUE;
  193.         break;
  194.  
  195.       /* change between system and oem font */
  196.     case IDM_SYSFONT:
  197.         MWnd.hFont = NULL;
  198.         CheckMenuItem(hMenu, IDM_SYSFONT, MF_CHECKED);
  199.         CheckMenuItem(hMenu, IDM_OEMFONT, MF_UNCHECKED);
  200.         break;
  201.  
  202.     case IDM_OEMFONT:
  203.         MWnd.hFont = GetStockObject(OEM_FIXED_FONT);
  204.         CheckMenuItem(hMenu, IDM_SYSFONT, MF_UNCHECKED);
  205.         CheckMenuItem(hMenu, IDM_OEMFONT, MF_CHECKED);
  206.         break;
  207.  
  208.       /* mask out bit 8 of each byte in file */
  209.     case IDM_MASK:
  210.         MWnd.ebitmask = 
  211.             (MWnd.ebitmask == (BYTE)0xff ? (BYTE)0x7f : (BYTE)0xff);
  212.         CheckMenuItem(hMenu, wParam,
  213.                    (MWnd.ebitmask == 0x7f ? MF_CHECKED : MF_UNCHECKED));
  214.         break;
  215.     }
  216. }
  217.  
  218. /* paint the main window */
  219. void NEAR MainWndPaint(hWnd, lpps)
  220. HWND hWnd;
  221. LPPAINTSTRUCT lpps;
  222. {
  223.  
  224.     HDC hDC = lpps->hdc;
  225.  
  226.   /* if the window is iconic, draw the icon */
  227.     if (IsIconic(hWnd)) {
  228.     RECT rIcon;
  229.     GetClientRect(hWnd, (LPRECT)&rIcon);
  230.     Rectangle(hDC, 0,0,rIcon.right, rIcon.bottom);
  231.         TextOut(hDC,2,rIcon.bottom/3,(LPSTR)szIconTitle,strlen(szIconTitle));
  232.     }
  233.   /* otherwise update the text in the window */
  234.     else 
  235.     TTYWndPaint(&MWnd, lpps->hdc, lpps->rcPaint.top, lpps->rcPaint.bottom);
  236. }
  237.  
  238. /* adjust where text is to be displayed if window is resized and redraw */
  239. void NEAR AdjustHeight(short width, short height)
  240. {
  241.      
  242.     MWnd.Width = width;
  243.     MWnd.Height = height;
  244.     MWnd.Pos.y = height - MWnd.CHeight - 1;
  245.     InvalidateRect(MWnd.hWnd, (LPRECT)NULL, TRUE);
  246. }
  247.  
  248. /* the following routine comes from the Microsoft TEMPLATE application */
  249. /*=============================================================================
  250.  DLGMERGESTRINGS scans string1 for merge spec (%%). If found, insert string2 at
  251.  that point, and then append remainder of string1.  Result in string3.
  252. ==============================================================================*/
  253. BOOL FAR DlgMergeStrings (szSrc, szMerge, szDst)
  254.  char    *szSrc;
  255.  char    *szMerge;
  256.  char    *szDst;
  257.  {
  258.     char *pchSrc;
  259.     char *pchDst;
  260.     unsigned int wMerge;
  261.  
  262.     wMerge = '%';
  263.     wMerge <<= 8;
  264.     wMerge |= '%';
  265.  
  266.     pchSrc = szSrc;
  267.     pchDst = szDst;
  268.  
  269.     /* Find merge spec if there is one. */
  270.     while (*(unsigned *)pchSrc != wMerge) {
  271.     *pchDst++ = *pchSrc;
  272.  
  273.     /* If we reach end of string before merge spec, just return. */
  274.     if (!*pchSrc++)
  275.         return FALSE;
  276.  
  277.     }
  278.     /* If merge spec found, insert sz2 there. (check for null merge string */
  279.     if (szMerge) {
  280.     while (*szMerge)
  281.         *pchDst++ = *szMerge++;
  282.  
  283.     }
  284.  
  285.     /* Jump over merge spec */
  286.     pchSrc++,pchSrc++;
  287.  
  288.  
  289.     /* Now append rest of Src String */
  290.     while (*pchDst++ = *pchSrc++);
  291.     return TRUE;
  292.  
  293. } /* end dlgmergestrings */
  294.